package com.game.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.game.json.JSONException;
import com.game.json.JSONObject;
import com.game.share.IOPackage;
import com.game.share.StreamWriter;

public class ActionGenerator {

	public static IOPackage get(String actionName, JSONObject object)
			throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		StreamWriter writer = new StreamWriter(out);
		writer.writeString(actionName);
		if (null != object) {
			writer.writeString(object.toString());
		} else {
			writer.writeString(null);
		}
		return new IOPackage(IOPackage.PackageType.REQUEST, out.toByteArray());
	}

	public static IOPackage GetGameListAction() throws IOException {
		return get("GetGameListAction", null);
	}

	public static IOPackage getHeartBreakAction() throws IOException {
		return get("HeartBreakAction", null);
	}

	public static IOPackage getLoginAction(String userName, String pwd)
			throws IOException {
		JSONObject object = new JSONObject();
		try {
			object.put("userName", userName);
			object.put("password", pwd);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return get("LoginAction", object);
	}

	public static IOPackage getValidateAction(String clientID)
			throws IOException {
		JSONObject object = new JSONObject();
		try {
			object.put("clientID", clientID);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return get("ValidateAction", object);
	}

}
